package com.eg366.nioLearn.baseSocket.chapter04;

import java.util.concurrent.TimeUnit;

public class ThreadExample implements Runnable {
	
	private String greeting;	// Message to print to console
	
	public ThreadExample(String greeting) {
		this.greeting = greeting;
	}

	@Override
	public void run() {
		while(true) {
			System.out.println(Thread.currentThread().getName() + ": " + greeting);
			try {
				TimeUnit.MILLISECONDS.sleep((long)Math.random() * 100);
			} catch (InterruptedException e) {
				/* Thread.sleep()方法可能会被其他线程打断，抛出InterruptedException异常 */
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		new Thread(new ThreadExample("aaa")).start();
		new Thread(new ThreadExample("bbb")).start();
		new Thread(new ThreadExample("ccc")).start();
	}
}
